package com.travel.photos.ui.find;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.ldoublem.loadingviewlib.LVChromeLogo;
import com.travel.R;
import com.travel.photos.bean.FindBean;
import com.travel.photos.bean.FindDataListBean;
import com.travel.photos.bean.RecommendBean;
import com.travel.photos.bean.RecommendDataListBean;
import com.travel.photos.config.UrlConfig;
import com.travel.photos.util.CustomDialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;


/**
 * Created by Administrator on 2016/10/17.
 */
public class FindPresenter implements FindContract.Presenter {

    private FindContract.View view;
    private FindContract.Model model;

    public FindPresenter(FindContract.View view) {
        this.view = view;
        this.model = new FindModel();
    }

    private String picId;
    private String pageSize = UrlConfig.DefaultVaule.PICID;
    private Map<String,String> findmap = new HashMap<>();
    private Map<String,String> remap = new HashMap<>();
    private List<FindDataListBean> finbeans;
    private List<RecommendDataListBean> rebeans;
    private FindAdapter findadapter;
   // private CustomDialog dialog;
    private ProgressDialog pd;

    @Override
    public void getDetail(final Context context) {
        findmap.put(UrlConfig.Params.PARAMTRPE,"get");
        findmap.put(UrlConfig.Params.PICID,pageSize);
        remap.put(UrlConfig.Params.PARAMTRPE,"get");

        model.getDetail(findmap,remap, new Subscriber<FindBean>() {
            @Override
            public void onCompleted() {
//                dialog.dismiss();
                pd.dismiss();
            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(context,"网络错误,小逗推荐换个wifi试试",Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNext(FindBean caipuDetail) {
                pd=new ProgressDialog(context);
                pd.setMessage("正在加载数据...");
                pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pd.show();
                /*dialog = new CustomDialog(context, R.style.CustomDialog);
                dialog.show();*/
                if ("0".equals(pageSize)){
                    finbeans = caipuDetail.getData().getList();
                    picId = caipuDetail.getData().getPicId();
                    findadapter= new FindAdapter(context,view,finbeans,rebeans);
                    view.getDetail(findadapter);
                }else {
                    finbeans.addAll(caipuDetail.getData().getList());
                }
            }
        },new Subscriber<RecommendBean>(){

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(context,"网络错误,小逗推荐换个wifi试试",Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNext(RecommendBean bean) {
                if ("0".equals(pageSize)){
                    rebeans = bean.getRecommendDataBean().getList();
                }else {
                    rebeans.addAll(bean.getRecommendDataBean().getList());
                    findadapter.notifyDataSetChanged();
                }
            }
        });
    }
    public void DownRefresh(Context context){
        pageSize = "0";
        getDetail(context);
    }

    public void PullUpRefresh(Context context){
        pageSize=picId;
        getDetail(context);
    }


    //public String getItemId(int i){
      //  return beans.get(i-1).getId()+"";
    //}
}
